<?php
/*
 *Module: RadLib_REQUEST.php
 *Description: Rad Extension Library for handling Server Request Vars
 */

// Library Class Definition
class REQUEST {
	public function tag_REQUEST_REQUEST_TYPE($node, $evalChild = TRUE) {
		$req_method = $_SERVER["REQUEST_METHOD"];
		$local = String::evalBool($node->getParam("local", FALSE));
		$node->assign("request_method", $req_method, $local);
		$node->assign("is_head_request", strcmp($req_method, "HEAD") === 0, $local);
		$node->assign("is_post_request", strcmp($req_method, "POST") === 0, $local);
		$node->assign("is_get_request", strcmp($req_method, "GET") === 0, $local);
		$node->assign("is_put_request", strcmp($req_method, "PUT") === 0, $local);
		if ($evalChild)
			$node->evalNodes();
	}

	public function tag_REQUEST_SERVER_VARS($node, $evalChild = TRUE) {
		$this->tag_REQUEST_REQUEST_TYPE($node, FALSE);
		$global = String::evalBool($node->getParam("global", FALSE));
		$var = $node->getParam("as");
		$stVar = $node->getParam("status");
		$status = NULL;
		if (String::isVarName($stVar)) {
			$status = new IteratorStatus();
			$status->setLength(count($_SERVER));
		}

		$i = 0;
		$array = NULL;
		foreach($_SERVER as $key => $value) {
			if (String::isVarName($stVar)) {
				$status->setKey($key);
				$status->setIndex($i);
				$node->assign($stVar, $status, TRUE);
			}

			if (String::isVarName($var)) {
				$array = array();
				$array[strtolower($key)] = $value;
				$node->assign($var, $array, TRUE);
			}

			if ($global)
				$node->assign(strtolower($key), $value, FALSE);
			$i++;
		}

		if ($evalChild)
			$node->evalNodes();
	}

	public function tag_REQUEST_GET_VARS($node) {
		$this->tag_REQUEST_SERVER_VARS($node, FALSE);
		$global = String::evalBool($node->getParam("global", FALSE));
		$var = $node->getParam("as");
		$stVar = $node->getParam("status");
		$status = NULL;
		if (String::isVarName($stVar)) {
			$status = new IteratorStatus();
			$status->setLength(count($_GET));
		}

		$i = 0;
		$array = NULL;
		foreach($_GET as $key => $value) {
			if (String::isVarName($stVar)) {
				$status->setKey($key);
				$status->setIndex($i);
				$node->assign($stVar, $status, TRUE);
			}

			if (String::isVarName($var)) {
				$array = array();
				$array[strtolower($key)] = htmlspecialchars($value);
				$node->assign($var, $array, TRUE);
			}

			if ($global)
				$node->assign(strtolower($key), htmlspecialchars($value), FALSE);
			$i++;
		}

		$node->evalNodes();
	}

	public function tag_REQUEST_POST_VARS($node) {
		$this->tag_REQUEST_SERVER_VARS($node, FALSE);
		$global = String::evalBool($node->getParam("global", FALSE));
		$var = $node->getParam("as");
		$stVar = $node->getParam("status");
		$status = NULL;
		if (String::isVarName($stVar)) {
			$status = new IteratorStatus();
			$status->setLength(count($_POST));
		}

		$i = 0;
		$array = NULL;
		foreach($_POST as $key => $value) {
			if (String::isVarName($stVar)) {
				$status->setKey($key);
				$status->setIndex($i);
				$node->assign($stVar, $status, TRUE);
			}

			if (String::isVarName($var)) {
				$array = array();
				$array[strtolower($key)] = htmlspecialchars($value);
				$node->assign($var, $array, TRUE);
			}

			if ($global)
				$node->assign(strtolower($key), htmlspecialchars($value), FALSE);
			$i++;
		}

		$node->evalNodes();
	}

	public function tag_REQUEST_FOREACH_SERVER($node) {
		$this->tag_REQUEST_SERVER_VARS($node, FALSE);
		$node->evalChildNode("header");
		$orig = strcasecmp($node->getParam("orig", "false"), "true") === 0;
		$var = $node->getParam("as");
		$stVar = $node->getParam("status");
		$status = NULL;
		if (String::isVarName($stVar)) {
			$status = new IteratorStatus();
			$status->setLength(count($_SERVER));
		}

		$i = 0;
		$array = NULL;
		foreach($_SERVER as $key => $value) {
			if (strpos($key, "ORIG_") === 0) {
				if (!$orig)
					continue;
			}

			if (String::isVarName($stVar)) {
				$status->setKey($key);
				$status->setIndex($i);
				$node->assign($stVar, $status, TRUE);
			}

			if (String::isVarName($var)) {
				$array = array();
				$array["server_var"] = $key;
				$array["server_val"] = $value;
				$node->assign($var, $array, TRUE);
			}

			$node->push();
			$node->evalChildNode("item");
			$node->pop();
			$i++;
		}

		$node->evalChildNode("footer");
	}

	public function tag_REQUEST_FOREACH_GET($node) {
		$this->tag_REQUEST_SERVER_VARS($node, FALSE);
		$node->evalChildNode("header");
		$len = count($_GET);
		$var = $node->getParam("as");
		$stVar = $node->getParam("status");
		$status = NULL;
		if (String::isVarName($stVar)) {
			$status = new IteratorStatus();
			$status->setLength($len);
		}

		$i = 0;
		$array = NULL;
		if ($len < 1) {
			$node->evalChildNode("empty");
		}
		else {
			foreach($_GET as $key => $value) {
				if (String::isVarName($stVar)) {
					$status->setKey($key);
					$status->setIndex($i);
					$node->assign($stVar, $status, TRUE);
				}

				if (String::isVarName($var)) {
					$array = array();
					$array["get_var"] = $key;
					$array["get_val"] = htmlspecialchars($value);
					$node->assign($var, $array, TRUE);
				}

				$node->push();
				$node->evalChildNode("item");
				$node->pop();
				$i++;
			}
		}

		$node->evalChildNode("footer");
	}

	public function tag_REQUEST_FOREACH_POST($node) {
		$this->tag_REQUEST_SERVER_VARS($node, FALSE);
		$node->evalChildNode("header");
		$len = count($_POST);
		$var = $node->getParam("as");
		$stVar = $node->getParam("status");
		$status = NULL;
		if (String::isVarName($stVar)) {
			$status = new IteratorStatus();
			$status->setLength($len);
		}

		$i = 0;
		$array = NULL;
		if (count($len) < 1) {
			$node->evalChildNode("empty");
		}
		else {
			foreach($_POST as $key => $value) {
				if (String::isVarName($stVar)) {
					$status->setKey($key);
					$status->setIndex($i);
					$node->assign($stVar, $status, TRUE);
				}

				if (String::isVarName($var)) {
					$array = array();
					$array["post_var"] = $key;
					$array["post_val"] = htmlspecialchars($value);
					$node->assign($var, $array, TRUE);
				}

				$node->push();
				$node->evalChildNode("item");
				$node->pop();
				$i++;
			}
		}

		$node->evalChildNode("footer");
	}

	public function __construct($plugin) {
		$plugin->setTagHandlerFunc("request_type"  , "tag_REQUEST_REQUEST_TYPE", $this);
		$plugin->setTagHandlerFunc("server_vars"   , "tag_REQUEST_SERVER_VARS", $this);
		$plugin->setTagHandlerFunc("get_vars"      , "tag_REQUEST_GET_VARS", $this);
		$plugin->setTagHandlerFunc("post_vars"     , "tag_REQUEST_POST_VARS", $this);
		$plugin->setTagHandlerFunc("foreach_server", "tag_REQUEST_FOREACH_SERVER", $this);
		$plugin->setTagHandlerFunc("foreach_get"   , "tag_REQUEST_FOREACH_GET", $this);
		$plugin->setTagHandlerFunc("foreach_post"  , "tag_REQUEST_FOREACH_POST", $this);
	}
}
?>